package com.markhsiu;

import com.alibaba.AAliServer;
import com.alibaba.BAliServer;
import com.markhsiu.bean.Machine;
import com.markhsiu.tools.FileUtils;

import java.io.BufferedReader;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author Mark Hsiu
 */
public class MachineAsys {


    public static void main(String[] args) throws Exception {
        getMachines(BAliServer.PATH_MACHINE);
    }

    public static final Map<String,Machine> getMachines(String path) throws Exception {
        Map<String,Machine> machineMap = new TreeMap<>();

        BufferedReader bs = FileUtils.readLines(path);

        for (String line = bs.readLine(); line != null; line = bs.readLine()) {
            String[] pair = line.split(",", -1);
            if (pair.length != 7) {
                throw new Exception("Invaild solution file");
            }

            String id = pair[0];
            String name = line.substring(id.length());
            Machine machine = machineMap.get(name);
            if(machine == null){
                machine = new Machine();
                machine.name  = name;
                machine.idList.add(id);
                machine.cpu = Integer.parseInt(pair[1]);
                machine.mem = Integer.parseInt(pair[2]);
                machine.disk = Integer.parseInt(pair[3]);
                machine.p = Integer.parseInt(pair[4]);
                machine.m = Integer.parseInt(pair[5]);
                machine.pm = Integer.parseInt(pair[6]);
                machineMap.put(name, machine);
            } else {
                machine.idList.add(id);
            }

        }

        for(Map.Entry<String, Machine> entry : machineMap.entrySet()){
            Machine machine = entry.getValue();
            System.out.println(entry.getKey());
            System.out.println(machine.idList.size());
            System.out.println(machine.cpu+" " +machine.mem+" "+machine.disk +" " +machine.p+" "+machine.m+" "+machine.pm);
            System.out.println(" ==== ");
        }

        return machineMap;
    }
}

